package solution.tree;

import common.TreeNode;
import common.util.TreeNodeUtil;

/**
 * @author : wenchao.long
 * @date : Created in 2021/7/9 16:30 周五
 * @description : 翻转一颗二叉树
 */
public class InvertTreeSolution {

    public TreeNode invertTree(TreeNode root) {
        return dfs(root);
    }

    private TreeNode dfs(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = dfs(root.left);
        root.left = dfs(root.right);
        root.right = left;
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = TreeNodeUtil.create(4, 2, 7, 1, 3, 6, 9);
        TreeNodeUtil.levelOrderList(root);

        InvertTreeSolution solution = new InvertTreeSolution();
        TreeNode tree = solution.invertTree(root);
        TreeNodeUtil.levelOrderList(tree);
    }


}
